using System.Runtime.InteropServices.Marshalling;
using System.Text;

namespace CSharpPro1._1016;

public class Demo
{
    //三个数从小到大输出
    public void TestPrint(int a, int b, int c)
    {
        int max = Math.Max(a, Math.Max(b, c));
        int min = Math.Min(a, Math.Min(b, c));
        if (a != max && a != min)
        {
            Console.Write(min+"\t");
            Console.Write(a+"\t");
            Console.Write(max);
        }

        if (b != max && b != min)
        {
            Console.Write(min+"\t");
            Console.Write(b+"\t");
            Console.Write(max);
        }

        if (c != max && c != min)
        {
            Console.Write(min+"\t");
            Console.Write(c+"\t");
            Console.Write(max);
        }
    }
    
    //打印1-100之间不是7的倍数，且不包含7的数字，并求和
    public void TestPrint2()
    {
        int sum = 0;
        for (int i = 1; i < 100; i++)
        {
            if (i%7==0||!NoHave7Number(i))
            {
                continue;
            }
            Console.Write(i+" ");
            sum += i;
        }
        Console.WriteLine();
        Console.WriteLine(sum);
    }

    private bool NoHave7Number(int n)
    {
        int temp = n;
        for (; temp > 0; temp /=10)
        {
            int digit = temp % 10;
            if (digit == 7)
            {
                return false;
            }
        }
        return true;
    }
    
    //打印九九乘法表
    public void TestPrint3()
    {
        for (int i = 1; i <= 9; i++)
        {
            for (int j = 1; j <= i; j++)
            {
                Console.Write($"{j}*{i}={j*i}"+" ");
            }
            Console.WriteLine();
        }
    }
    
    //除了自身 外的所有因数之和等于其本身，为完数
    public void TestPrint4()
    {
        static bool IsPerfectNumber(int n)
        {
            int sum = 0;
            for (int i = 1; i <= n; i++)
            {
                if (n % i == 0)
                {
                    sum += i;
                }
            }

            return sum == 2 * n;
        }

        for (int i = 1; i <= 1000; i++)
        {
            if (IsPerfectNumber(i))
            {
                Console.WriteLine(i);
            }
        }
    }
    
    //键盘录入一个正整数，请求：1.它是几位数？2.逆序打印出各位数字。
    public void TestPrint5()
    {
        Console.WriteLine("请输入一个正整数：");
        if (!int.TryParse(Console.ReadLine(), out int num) || num <= 0)
        {
            Console.WriteLine("输入错误，请输入正整数！");
            return;
        }

        //
        int count = 0;
        StringBuilder result = new StringBuilder();
        int temp = num;
        
        while (temp > 0)
        {
            int digit = temp % 10;
            count++;
            result.Append(digit);
            temp /= 10;
        }

        // 输出结果
        Console.WriteLine($"该数字是 {count} 位数");
        Console.WriteLine($"逆序打印：{result}");
    }

    //打印菱形
    public void TestPrint6()
    {
        string? m = Console.ReadLine();
        int n = int.Parse(m);
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < i; j++)
            {
                Console.Write(" ");
            }

            for (int k = 0; k < n; k++)
            {
                Console.Write("* ");
            }
            Console.WriteLine();
        }
    }
}